﻿using System;
using System.Globalization;


namespace FireBus.Contracts
{
	[Serializable]
	public abstract class ValueObject<T>
	{
		private T _Value;

		protected T Value
		{
			get { return _Value; }
			set { _Value = value; }
		}

		protected Boolean IsNull
		{
			get { return Equals(_Value, null); }
		}

		public override String ToString()
		{
			return IsNull ? String.Empty : String.Format(CultureInfo.InvariantCulture, "{0}", _Value);
		}

		public override Int32 GetHashCode()
		{
			return IsNull ? 0 : _Value.GetHashCode();
		}

		public override Boolean Equals(Object obj)
		{
			return Equals(obj as ValueObject<T>);
		}

		protected Boolean Equals(ValueObject<T> other)
		{
			if (other == null)
			{
				return false;
			}
			if (ReferenceEquals(this, other))
			{
				return true;
			}
			return Equals(
				_Value, other._Value);
		}
	}
}
